﻿using System;
using System.Windows.Data;

namespace JulMar.Silverlight.Converters
{
    /// <summary>
    /// This converts a double into an integer value, rounding the value off.
    /// It is useful when providing textual versions of scrollbar or slider values (on a tooltip).
    /// </summary>
    public class DoubleToIntegerConverter : IValueConverter
    {
        #region IValueConverter Members

        object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return DoConvert(value, targetType);
        }

        object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return DoConvert(value, targetType);
        }

        #endregion

        private static object DoConvert(object value, Type targetType)
        {
            if (value != null)
            {
                if (value.GetType() == typeof(double))
                    return Convert.ToInt32((double) value);
                if (value.GetType() == typeof(int))
                    return Convert.ToDouble((int)value);
            }

            return value;
        }
    }
}


